
//主页面控制器
food.controller('FoodListController',function($scope,$http,page,$rootScope){
   
    // 声明一个变量
    $rootScope.food_id = 0;

    //获取总条数
    $http({
        method:'GET',
        url:'../Api/admin.php?action=food_count'
    }).then(function(result){
        $scope.totalItems = result.data.c;
    });

    $scope.currentPage = 1;
    $scope.bigTotalItems = 10;
    $scope.maxSize = 5;

    var url = "../Api/admin.php?action=food_list";
    
    // 默认第一页的页码  数据默认第一页显示
    page.getData(url,$scope.currentPage,$scope.bigTotalItems).then(function(result){
        $scope.food_list = result;
    });
    
    // 页码改变时 数据根据页码改变而改变
    $scope.pageChanged = function(){
        page.getData(url,$scope.currentPage,$scope.bigTotalItems).then(function(result){
            $scope.food_list = result;
        });
    };
   
   // 该方法接收到 点击删除名令传过来的id
    $scope.deleteFood = function(food_id){
         // 全局变量  意味着整个页面都可以使用这个变量
        $rootScope.food_id = food_id;
    }


});

food.controller('FoodAddController',function($scope,$http,Upload,$location){

    //查询食品分类
    $http({
        method:"GET",
        url:"../Api/admin.php?action=food_cate"
    }).then(function(result){
        $scope.cate_list = result.data;
    });


    //上传文件
    $scope.uploadImg = function(){

        if($scope.food.food_img){
            console.log($scope.food.food_img);
            Upload.upload({
                url:"../Api/admin.php?action=food_add",

                file:$scope.food.food_img

            }).success(function(result){
                $scope.food.food_img = "uploads/"+result.food_img;
            });
        }
    };

    //提交表单  点击添加表单的方法 post提交
    $scope.saveForm = function(){

        $http({
            method:"POST",
            url:"../Api/admin.php?action=food_add",
            data:$scope.food,
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            transformRequest:function(obj){
                var str = [];
                for(var s in obj){

                    str.push(encodeURIComponent(s) + "=" + encodeURIComponent(obj[s]));
                }

                return str.join("&");
            }


        }).then(function(result){
            if(result.data){
                $location.path('food_list');
            }else{
                alert('添加失败');
                $location.path('food_add');
            }
        });
    }

});





//编辑食品
food.controller('FoodEditController',function($scope,$http,Upload,$location,$stateParams,time){


    //查询出当前产品
    $http({
        method:"get",
        url:"../Api/admin.php?action=food_detail&food_id="+$stateParams.food_id
    }).then(function(result){
        $scope.food = result.data;
        $scope.food.food_time = time.timestampToTime($scope.food.food_time);
    });

    //查询食品分类
    $http({
        method:"GET",
        url:"../Api/admin.php?action=food_cate"
    }).then(function(result){

        $scope.cate_list = result.data;
    });


    //上传文件
    $scope.uploadImg = function()
    {
        if($scope.food.food_img){
            // 返回文件名称
            console.log($scope.food.food_img);
            // 进度条插件
            Upload.upload({
                url:"../Api/admin.php?action=food_edit",//返回图片路径 {'img':'404.jpg'}
                file:$scope.food.food_img
            }).success(function(result){
                $scope.food.food_img = "uploads/"+result.food_img;//存到本地文件夹
            });
        }
    };

    //提交表单 通过事件提交表单
    $scope.saveForm = function(){
        $http({
            method:"POST",
            url:"../Api/admin.php?action=food_edit&food_id="+$stateParams.food_id,
            data:$scope.food,
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            transformRequest:function(obj){
                var str = [];
                for(var s in obj)
                {
                    str.push(encodeURIComponent(s) + "=" + encodeURIComponent(obj[s]));
                }

                return str.join("&");
            }
        }).then(function(result){
            if(result.data)
            {
                $location.path('food_list');
            }else{
                alert('添加失败');
                $location.path('food_edit/'+$stateParams.food_id);
            }
        });
    }

});

//删除食品
food.controller("FoodDeleteController",function($scope,$http,$stateParams,$location){

    $http({
        url:"../Api/admin.php?action=food_delete&food_id="+$stateParams.food_id,
        method:"get"
    }).then(function(result){
         console.log(result);


        if(result.data){
            $location.path('food_list');

        }else{
            alert('删除失败');
            $location.path('food_list');
        }
    });
});


food.controller('CateFoodListController',function($http,$scope,page){
 
 // 查询总数

 $http({
  
   method:'get',
   url:'../Api/admin.php?action=catefood_count'

 }).then(function(res){

      
    $scope.totalItems=res.data.c;

 });

 //组装分页

 $scope.currentPage=1;
 $scope.bigTotalItems=8;
 $scope.maxSize=5;
 var url="../Api/admin.php?action=catefood_list";
 page.getData(url,$scope.currentPage,$scope.bigTotalItems).then(function(res){
   $scope.catefood_list=res;
 });

$scope.pageChanged=function(){
    page.getData(url,$scope.currentPage,$scope.bigTotalItems).then(function(res){
   $scope.catefood_list=res;
 });

};



});

food.controller('CateFoodAddController',function($scope,$http,$location){

   $scope.saveForm = function(){

        $http({
            method:"POST",
            url:"../Api/admin.php?action=catefood_add",
            data:$scope.catefood,

            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            transformRequest:function(obj){
                var str = [];
                for(var s in obj){
                    str.push(encodeURIComponent(s) + "=" + encodeURIComponent(obj[s]));
                }
                return str.join("&");
            }
        }).then(function(result){

            if(result.data){
                $location.path('catefood_list');
            }else{
                alert('添加失败');
                $location.path('catefood_list');
            }
        });
    }




});

// 食品分类的编辑

food.controller('CateFoodEditController',function($scope,$http,$location,time,$stateParams){

// 显示旧信息
 $http({
  
  method:'get',
  url:'../Api/admin.php?action=catefood_detile&fcate_id='+$stateParams.fcate_id


 }).then(function(res){
   
   
  $scope.catefood=res.data;

   $scope.catefood.fcate_time = time.timestampToTime($scope.catefood.fcate_time);

 });

 $scope.saveForm = function(){

        $http({
            method:"POST",
            url:'../Api/admin.php?action=catefood_edit&fcate_id='+$stateParams.fcate_id,
            // 提交的数据
            data:$scope.catefood,
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            transformRequest:function(obj){
                var str = [];
                for(var s in obj){

                    str.push(encodeURIComponent(s) + "=" + encodeURIComponent(obj[s]));
                }

                return str.join("&");
            }
        }).then(function(result){
            if(result.data){
                $location.path('catefood_list');
            }else{
                alert('修改失败');
                $location.path('catefood_list');
            }
        });
    }

// 修改后提交的信息


});


food.controller('CateFoodDeleteController',function($http,$scope,$location,$stateParams){

  $http({

    method:'get',
    url:'../Api/admin.php?action=catefood_delete&fcate_id='+$stateParams.fcate_id
  }).then(function(res){
    if(res.data){

                $location.path('catefood_list');
            }else{
                alert('删除失败');
                $location.path('catefood_list');
            }

  });

})